package com.tbit.main.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tbit.main.constant.enums.WeatherEnum;
import com.tbit.main.dao.data.WeatherHistoryDao;
import com.tbit.main.dao.log.AccountWeatherDao;
import com.tbit.main.pojo.AccountWeather;
import com.tbit.main.pojo.WeatherDate;
import com.tbit.main.pojo.Weatherhistory;
import com.tbit.main.pojo.entity.WeatherGroup;
import com.tbit.main.service.AccountWeatherService;
import com.tbit.main.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: chen
 * @Description:
 * @Date: 创建于 14:05 2023/3/6
 */
@Service
public class AccountWeatherServiceImpl extends ServiceImpl<AccountWeatherDao, AccountWeather> implements AccountWeatherService {

    @Autowired
    private WeatherHistoryDao weatherHistoryDao;
    @Autowired
    private AccountWeatherDao accountWeatherDao;

    public static void main(String[] args) {
        System.out.println(DateTimeUtil.getNowDateStringBefore(DateTimeUtil.getNowTime(), -1));
    }

    @Override
    public List<Weatherhistory> getHoursWeather(Integer accountId, String startTime, String endTime) {
        endTime = DateTimeUtil.getNowTime();
        startTime = DateTimeUtil.getNowDateStringBefore(DateTimeUtil.getNowTime(), -1);
        Map<String, Object> map = new HashMap<>();
        map.put("accountId", accountId);
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        List<Weatherhistory> weatherhistories = weatherHistoryDao.getByAccountId(map);

        Map<Integer, Weatherhistory> weatherhistoryMap = new HashMap<>();
        for (Weatherhistory weatherhistory : weatherhistories) {
            weatherhistoryMap.put(weatherhistory.getHour(), weatherhistory);
        }

        /**获取所有时间*/
        List<Integer> hourList = DateTimeUtil.getAllHour();
        if (weatherhistories != null && !weatherhistories.isEmpty()) {
            weatherhistories.forEach(weatherhistory -> {
                weatherhistory
                        .setSkyIcon(WeatherEnum.transFanType(Integer.valueOf(weatherhistory.getWeather())));
                weatherhistory.setWeatherText(
                        WeatherEnum.getNameByCode(Integer.valueOf(weatherhistory.getWeather())));
                weatherhistory.setIsNull(0);
            });
        }

        hourList.forEach(hour -> {
            if (!weatherhistoryMap.containsKey(hour)) {
                weatherhistories.add(new Weatherhistory(accountId, hour, null, null, null, null, null, null, 1));
            }
        });
        return weatherhistories;
    }

    @Override
    public List<WeatherDate> getDaysWeather(Integer accountId, String startTime, String endTime) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("accountId", accountId);
        queryWrapper.ge("addTime", startTime);
        queryWrapper.le("addTime", endTime);

        List<AccountWeather> accountWeathers = list(queryWrapper);
        List<WeatherDate> weatherDates = new ArrayList<>();

        /**数据集合*/
        Map<String, AccountWeather> accountWeatherHashMap = new HashMap<>();
        for (AccountWeather accountWeather : accountWeathers) {
            accountWeatherHashMap.put(accountWeather.getDate(), accountWeather);
        }
        /**时间集合*/
        List<String> weatherDateList = DateTimeUtil.findDates(startTime, endTime);
        weatherDateList.forEach(accountWeather -> {
            AccountWeather accountWeatherDate = accountWeatherHashMap.get(accountWeather);
            WeatherDate weatherDate = new WeatherDate();
            weatherDate.setLocalDate(accountWeather);
            if (accountWeatherDate != null) {
                weatherDate.setMaxTemp(accountWeatherDate.getTempMax());
                weatherDate.setMinTemp(accountWeatherDate.getTempMin());
                weatherDate
                        .setSkyIcon(WeatherEnum.transFanType(Integer.valueOf(accountWeatherDate.getIconDay())));
                weatherDate
                        .setWeatherText(WeatherEnum.getNameByCode(Integer.valueOf(accountWeatherDate.getIconDay())));
                weatherDate.setIsNull(0);
            } else {
                weatherDate.setIsNull(1);
            }
            weatherDates.add(weatherDate);
        });
        return weatherDates;
    }

    @Override
    public Map<String, String> groupDaysWeather(Integer accountId, String startTime,
                                                String endTime) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("accountId", accountId);
        queryWrapper.gt("addTime", startTime);
        queryWrapper.le("addTime", endTime);
        List<AccountWeather> accountWeathers = list(queryWrapper);
        Map<String, String> map = new HashMap<>();
        List<Integer> normal = WeatherEnum.getNormalType();
        Integer normalNum = 0;
        Integer abNormalNum = 0;
        for (AccountWeather accountWeather : accountWeathers) {
            if (normal.contains(WeatherEnum.transFanType(accountWeather.getIconDay()))) {
                normalNum = normalNum + 1;
            } else {
                abNormalNum = abNormalNum + 1;
            }
        }
        map.put("normalDays", normalNum.toString());
        map.put("abnormalDays", abNormalNum.toString());
        return map;
    }

    @Override
    public List<Map<String, String>> groupByTime(Integer accountId, String startTime, String endTime) {
        Map<String, Object> map = new HashMap<>();
        map.put("accountId", accountId);
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        List<WeatherGroup> groups = accountWeatherDao.groupByTime(map);
        /**按周数分组*/
        Map<String, List<WeatherGroup>> listMap = groups.stream().collect(Collectors.groupingBy(WeatherGroup::getWeek));
        /**正常天气*/
        List<Integer> normal = WeatherEnum.getNormalType();

        List<Map<String, String>> result = new ArrayList<>();
        /**查询统计数据*/
        Map<String, String> map1 = DateTimeUtil.getDayOfWeekWithinDateInterval(startTime, endTime, 1);

        /**预置数据*/
        Map<String, Map<String, String>> map2 = new HashMap<>();
        for (String key : map1.keySet()) {
            if (!listMap.keySet().contains(key)) {
                Map<String, String> groupMap = new HashMap<>();
                groupMap.put("time", map1.get(key));
                groupMap.put("normalDays", "0");
                groupMap.put("abnormalDays", "0");
                //map2.put(key,groupMap);
                result.add(groupMap);
            }
        }
        /**构造数据*/
        for (String key : listMap.keySet()) {
            Map<String, String> groupMap = new HashMap<>();
            Integer normalNum = 0;
            Integer abNormalNum = 0;
            for (WeatherGroup weatherGroup : listMap.get(key)) {
                if (normal.contains(WeatherEnum.transFanType(weatherGroup.getWeather()))) {
                    normalNum = normalNum + weatherGroup.getNum();
                } else {
                    abNormalNum = abNormalNum + weatherGroup.getNum();
                }
            }
            groupMap.put("time", map1.get(key));
            groupMap.put("normalDays", normalNum.toString());
            groupMap.put("abnormalDays", abNormalNum.toString());
            result.add(groupMap);
        }
        return result;
    }

    @Override
    public List<Map<String, String>> groupByMonth(Integer accountId, String startTime,
                                                  String endTime) {
        Map<String, Object> map = new HashMap<>();
        map.put("accountId", accountId);
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        List<WeatherGroup> groups = accountWeatherDao.groupByMonth(map);
        /**按月数分组*/
        Map<String, List<WeatherGroup>> listMap = groups.stream().collect(Collectors.groupingBy(WeatherGroup::getWeek));
        /**正常天气*/
        List<Integer> normal = WeatherEnum.getNormalType();
        List<Map<String, String>> result = new ArrayList<>();

        /**查询统计数据*/
        List<String> list = DateTimeUtil.getMonthDay(startTime, endTime);

        /**预置数据*/
        //Map<String, Map<String, String>> map2 = new HashMap<>();
        for (String key : list) {
            if (!listMap.keySet().contains(key)) {
                Map<String, String> groupMap = new HashMap<>();
                groupMap.put("time", key);
                groupMap.put("normalDays", "0");
                groupMap.put("abnormalDays", "0");
                //map2.put(key,groupMap);
                result.add(groupMap);
            }
        }

        /**构造数据*/
        for (String key : listMap.keySet()) {
            Map<String, String> groupMap = new HashMap<>();
            Integer normalNum = 0;
            Integer abNormalNum = 0;
            for (WeatherGroup weatherGroup : listMap.get(key)) {
                if (normal.contains(WeatherEnum.transFanType(weatherGroup.getWeather()))) {
                    normalNum = normalNum + weatherGroup.getNum();
                } else {
                    abNormalNum = abNormalNum + weatherGroup.getNum();
                }
            }
            groupMap.put("time", key);
            groupMap.put("normalDays", normalNum.toString());
            groupMap.put("abnormalDays", abNormalNum.toString());
            result.add(groupMap);
        }
        return result;
    }

}
